package com.suep.business.mapper;

import com.suep.business.domain.Order;
import com.suep.business.enums.OrderStatus;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {


    //将订单切换到下一个状态
    @Update("UPDATE `order` SET  status=#{orderStatus} where order_id=#{orderId}")
    Integer updateOrderNextStatus(@Param("orderStatus") OrderStatus orderStatus,@Param("orderId") Long orderId);

    //查询用户正在进行中的订单
    @Select("SELECT * FROM `order` WHERE status in('PAID','STORE_RECEIVED','DELIVERYMAN_RECEIVED','DELIVERYMAN_COMPLETED') AND user_id=#{userId}")
    List<Order> selectOrderListInProgress(Integer userId);

    @Select("SELECT * FROM `order` WHERE store_id=#{storeId} AND DATE(payment_time) = CURDATE()")
    List<Order> selectOrderListByStoreIdByToday(@Param("storeId") Integer storeId);

    // 主键精确查询
    @Select("SELECT * FROM `order` WHERE order_id = #{orderId}")
    Order selectOrderById(@Param("orderId") Long orderId);

    // 按用户id外键查询
    @Select("SELECT * FROM `order` WHERE user_id = #{userId}")
    List<Order> selectOrderByUserId(@Param("userId") Integer userId);

    // 按店铺id外键查询
    @Select("SELECT * FROM `order` WHERE store_id = #{storeId}")
    List<Order> selectOrderByStoreId(@Param("storeId") Integer storeId);




    // 按配送员id外键查询
    @Select("SELECT * FROM `order` WHERE delivery_man_id = #{deliveryManId}")
    List<Order> selectOrderByDeliveryManId(@Param("deliveryManId") Integer deliveryManId);

    // 主键精确删除
    @Delete("DELETE FROM `order` WHERE order_id = #{orderId}")
    Integer deleteOrderById(@Param("orderId") Integer orderId);

    // 插入
    @Insert("INSERT INTO `order` (order_id,user_id, store_id, address_id,address, coupon_id,coupon_name,delivery_man_id, total_price, actual_price, order_time, payment_time, delivery_time, complete_time, status) " +
            "VALUES (#{orderId},#{userId}, #{storeId}, #{addressId},#{address}, #{couponId},#{couponName} ,#{deliveryManId}, #{totalPrice}, #{actualPrice}, #{orderTime}, #{paymentTime}, #{deliveryTime}, #{completeTime}, #{status})")
    Integer insertOrder(Order order);

    // 更新
    @Update("UPDATE `order` SET user_id = #{user_id}, store_id = #{storeId}, address_id = #{addressId},address=#{address}, coupon_id = #{coupon_id},coupon_name=#{couponName}, delivery_man_id = #{deliveryManId}, " +
            "total_price = #{totalPrice}, actual_price = #{actualPrice}, order_time = #{orderTime}, payment_time = #{paymentTime}, delivery_time = #{deliveryTime}, " +
            "complete_time = #{completeTime}, status = #{status} WHERE order_id = #{orderId}")
    Integer updateOrderById(Order order);
}